/*
 * SPDX-FileCopyrightText: 2020 Kyunghwan Kwon <k@mononn.com>
 *
 * SPDX-License-Identifier: MIT
 */

#include "CppUTest/TestHarness.h"

extern "C" {
#include "fake_storage.h"
#include <string.h>
}

static char spy_buffer[256];

TEST_GROUP(FakeStorage) {
	const logging_storage_t *storage;

	void setup(void) {
		storage = logging_fake_storage_init(print_string);
	}
	void teardown(void) {
	}

	static int print_string(const char *s) {
		strcpy(spy_buffer, s);
		return 0;
	}
};

TEST(FakeStorage, write_ShouldConvertDataToString) {
	const char *fixed_string = "1599652568: [VERBOSE] <0x558d66c5d4db,0x558d66c6deb2> Hello, World";
	uint8_t data[] = {
		0xd8, 0xc2, 0x58, 0x5f, 0x00, 0x00, 0x00, 0x00,
		0xdb, 0xd4, 0xc5, 0x66, 0x8d, 0x55, 0x00, 0x00,
		0xb2, 0xde, 0xc6, 0x66, 0x8d, 0x55, 0x00, 0x00,
		0xcc, 0xaf, 0x00, 0x0c, 0x48, 0x65, 0x6c, 0x6c,
		0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64,
	};
	CHECK_EQUAL(sizeof(data), storage->write(data, sizeof(data)));
	STRCMP_EQUAL(fixed_string, spy_buffer);
}

TEST(FakeStorage, write_ShouldConvertDataToString_WhenTypeIsDebug) {
	const char *fixed_string = "1599652568: [DEBUG] <0x558d66c5d4db,0x558d66c6deb2> Hello, World";
	uint8_t data[] = {
		0xd8, 0xc2, 0x58, 0x5f, 0x00, 0x00, 0x00, 0x00,
		0xdb, 0xd4, 0xc5, 0x66, 0x8d, 0x55, 0x00, 0x00,
		0xb2, 0xde, 0xc6, 0x66, 0x8d, 0x55, 0x00, 0x00,
		0xcc, 0xaf, 0x01, 0x0c, 0x48, 0x65, 0x6c, 0x6c,
		0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64,
	};
	CHECK_EQUAL(sizeof(data), storage->write(data, sizeof(data)));
	STRCMP_EQUAL(fixed_string, spy_buffer);
}

TEST(FakeStorage, write_ShouldConvertDataToString_WhenTypeIsInfo) {
	const char *fixed_string = "1599652568: [INFO] <0x558d66c5d4db,0x558d66c6deb2> Hello, World";
	uint8_t data[] = {
		0xd8, 0xc2, 0x58, 0x5f, 0x00, 0x00, 0x00, 0x00,
		0xdb, 0xd4, 0xc5, 0x66, 0x8d, 0x55, 0x00, 0x00,
		0xb2, 0xde, 0xc6, 0x66, 0x8d, 0x55, 0x00, 0x00,
		0xcc, 0xaf, 0x02, 0x0c, 0x48, 0x65, 0x6c, 0x6c,
		0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64,
	};
	CHECK_EQUAL(sizeof(data), storage->write(data, sizeof(data)));
	STRCMP_EQUAL(fixed_string, spy_buffer);
}

TEST(FakeStorage, write_ShouldConvertDataToString_WhenTypeIsNotice) {
	const char *fixed_string = "1599652568: [NOTICE] <0x558d66c5d4db,0x558d66c6deb2> Hello, World";
	uint8_t data[] = {
		0xd8, 0xc2, 0x58, 0x5f, 0x00, 0x00, 0x00, 0x00,
		0xdb, 0xd4, 0xc5, 0x66, 0x8d, 0x55, 0x00, 0x00,
		0xb2, 0xde, 0xc6, 0x66, 0x8d, 0x55, 0x00, 0x00,
		0xcc, 0xaf, 0x03, 0x0c, 0x48, 0x65, 0x6c, 0x6c,
		0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64,
	};
	CHECK_EQUAL(sizeof(data), storage->write(data, sizeof(data)));
	STRCMP_EQUAL(fixed_string, spy_buffer);
}

TEST(FakeStorage, write_ShouldConvertDataToString_WhenTypeIsWarn) {
	const char *fixed_string = "1599652568: [WARN] <0x558d66c5d4db,0x558d66c6deb2> Hello, World";
	uint8_t data[] = {
		0xd8, 0xc2, 0x58, 0x5f, 0x00, 0x00, 0x00, 0x00,
		0xdb, 0xd4, 0xc5, 0x66, 0x8d, 0x55, 0x00, 0x00,
		0xb2, 0xde, 0xc6, 0x66, 0x8d, 0x55, 0x00, 0x00,
		0xcc, 0xaf, 0x04, 0x0c, 0x48, 0x65, 0x6c, 0x6c,
		0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64,
	};
	CHECK_EQUAL(sizeof(data), storage->write(data, sizeof(data)));
	STRCMP_EQUAL(fixed_string, spy_buffer);
}

TEST(FakeStorage, write_ShouldConvertDataToString_WhenTypeIsError) {
	const char *fixed_string = "1599652568: [ERROR] <0x558d66c5d4db,0x558d66c6deb2> Hello, World";
	uint8_t data[] = {
		0xd8, 0xc2, 0x58, 0x5f, 0x00, 0x00, 0x00, 0x00,
		0xdb, 0xd4, 0xc5, 0x66, 0x8d, 0x55, 0x00, 0x00,
		0xb2, 0xde, 0xc6, 0x66, 0x8d, 0x55, 0x00, 0x00,
		0xcc, 0xaf, 0x05, 0x0c, 0x48, 0x65, 0x6c, 0x6c,
		0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64,
	};
	CHECK_EQUAL(sizeof(data), storage->write(data, sizeof(data)));
	STRCMP_EQUAL(fixed_string, spy_buffer);
}

TEST(FakeStorage, write_ShouldConvertDataToString_WhenTypeIsAlert) {
	const char *fixed_string = "1599652568: [ALERT] <0x558d66c5d4db,0x558d66c6deb2> Hello, World";
	uint8_t data[] = {
		0xd8, 0xc2, 0x58, 0x5f, 0x00, 0x00, 0x00, 0x00,
		0xdb, 0xd4, 0xc5, 0x66, 0x8d, 0x55, 0x00, 0x00,
		0xb2, 0xde, 0xc6, 0x66, 0x8d, 0x55, 0x00, 0x00,
		0xcc, 0xaf, 0x06, 0x0c, 0x48, 0x65, 0x6c, 0x6c,
		0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64,
	};
	CHECK_EQUAL(sizeof(data), storage->write(data, sizeof(data)));
	STRCMP_EQUAL(fixed_string, spy_buffer);
}

TEST(FakeStorage, write_ShouldConvertDataToString_EvenWhenUnknownType) {
	const char *fixed_string = "1599652568: [UNKNOWN] <0x558d66c5d4db,0x558d66c6deb2> Hello, World";
	uint8_t data[] = {
		0xd8, 0xc2, 0x58, 0x5f, 0x00, 0x00, 0x00, 0x00,
		0xdb, 0xd4, 0xc5, 0x66, 0x8d, 0x55, 0x00, 0x00,
		0xb2, 0xde, 0xc6, 0x66, 0x8d, 0x55, 0x00, 0x00,
		0xcc, 0xaf, 0xff, 0x0c, 0x48, 0x65, 0x6c, 0x6c,
		0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64,
	};
	CHECK_EQUAL(sizeof(data), storage->write(data, sizeof(data)));
	STRCMP_EQUAL(fixed_string, spy_buffer);
}

TEST(FakeStorage, read_ShouldReturnZero_WhenAlways) {
	CHECK_EQUAL(0, storage->read(NULL, 0));
}

TEST(FakeStorage, consume_ShouldReturnZero_WhenAlways) {
	CHECK_EQUAL(0, storage->consume(1));
}

TEST(FakeStorage, count_ShouldReturnZero_WhenAlways) {
	CHECK_EQUAL(0, storage->count());
}
